<template>
  <div class="table-box">
    <ProTable ref="proTable" :columns="columns" :request-api="queryCustomersPageList">
      <!-- 表格 header 按钮 -->
      <template #tableHeader>
        <el-button type="primary" :icon="CirclePlus" @click="insertDialogVisible = true">新增客户</el-button>
      </template>
      <template #createDate="scope">
        {{ scope.row.createDate.replace("T", " ").split(".")[0] }}
      </template>
      <template #updateDate="scope">
        {{ scope.row.updateDate.replace("T", " ").split(".")[0] }}
      </template>
      <!-- 表格操作 -->
      <template #operation="scope">
        <el-button type="primary" link :icon="EditPen" @click="openUpdateDialog(scope.row)">编辑</el-button>
        <el-button type="primary" link :icon="Delete" @click="removeCustomers(scope.row)">删除</el-button>
      </template>
    </ProTable>
    <!-- 新增对话框 -->
    <el-dialog width="30%" v-model="insertDialogVisible" title="新增客户">
      <el-form ref="insertFormRef" :model="insertParam" :rules="ruleForm" label-width="150px" class="demo-ruleForm">
        <el-form-item label="客户名称" prop="customersName">
          <el-input v-model="insertParam.customersName"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="insertParam.status">
            <el-radio :label="0" :value="0">禁用</el-radio>
            <el-radio :label="1" :value="1">正常</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="联系方式" prop="telephone">
          <el-input v-model="insertParam.telephone"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="insertParam.email"></el-input>
        </el-form-item>
        <el-form-item label="联系地址" prop="address">
          <el-input v-model="insertParam.address"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="insertDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="addCustomers(insertFormRef)">新增</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 修改对话框 -->
    <el-dialog width="30%" v-model="updateDialogVisible" title="修改客户信息">
      <el-form ref="updateFormRef" :model="updateParam" :rules="ruleForm" label-width="150px" class="demo-ruleForm">
        <el-form-item label="客户名称" prop="customersName">
          <el-input v-model="updateParam.customersName"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="updateParam.status">
            <el-radio :label="0" :value="0">禁用</el-radio>
            <el-radio :label="1" :value="1">正常</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="联系方式" prop="telephone">
          <el-input v-model="updateParam.telephone"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="updateParam.email"></el-input>
        </el-form-item>
        <el-form-item label="联系地址" prop="address">
          <el-input v-model="updateParam.address"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="updateDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="updateCustomers(updateFormRef)">修改</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup lang="ts" name="customers">
import { ref, reactive, onMounted } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import type { FormInstance, FormRules } from "element-plus";
import { CirclePlus, Delete, EditPen, Download, Upload, View, Refresh, ElementPlus } from "@element-plus/icons-vue";
import ProTable from "@/components/ProTable/index.vue";
import { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import { statusEnum, statusFieldNames } from "@/enums/httpEnum";
import { createCustomers, deleteCustomers, editCustomers, queryCustomersPageList } from "@/api/modules/customers";
import { CreateCustomersParam, EditCustomersParam, HxCustomers } from "@/api/interface/customers";

const proTable = ref<ProTableInstance>();

const insertDialogVisible = ref(false);
const updateDialogVisible = ref(false);

const insertFormRef = ref<FormInstance>();

const updateFormRef = ref<FormInstance>();

const insertParam = reactive<CreateCustomersParam>({
  customersName: "",
  status: 1
});

const updateParam = reactive<EditCustomersParam>({
  customersId: "",
  customersName: "",
  status: 1
});

/** form表单验证规则 */
const ruleForm = reactive<FormRules>({
  customersName: [{ required: true, message: "请输入客户名称", trigger: "change" }],
  telephone: [{ required: true, message: "请输入联系方式", trigger: "change" }],
  email: [{ required: true, message: "请输入邮箱地址", trigger: "change" }],
  address: [{ required: true, message: "请输入联系地址", trigger: "change" }]
});

const columns = reactive<ColumnProps<HxCustomers>[]>([
  { type: "index", label: "", width: 80 },
  {
    prop: "customersName",
    label: "客户名称",
    width: 150,
    search: { el: "input", tooltip: "请输入客户名称" }
  },

  {
    prop: "status",
    label: "状态",
    tag: true,
    enum: statusEnum,
    search: { el: "select", props: { filterable: true } },
    fieldNames: statusFieldNames
  },
  {
    prop: "telephone",
    label: "联系方式"
  },
  {
    prop: "email",
    label: "邮箱"
  },
  {
    prop: "address",
    label: "联系地址",
    width: 130
  },
  {
    prop: "createDate",
    label: "创建时间",
    width: 200
  },
  {
    prop: "updateDate",
    label: "修改时间",
    width: 200
  },
  { prop: "operation", label: "操作", fixed: "right", width: 330 }
]);

/** 打开修改对话框 */
const openUpdateDialog = (param: HxCustomers) => {
  updateDialogVisible.value = true;
  Object.assign(updateParam, param);
};

/** 新增客户信息 */
const addCustomers = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      await createCustomers(insertParam);
      ElMessage.success("新增商品成功");
      insertDialogVisible.value = false;
      insertParam.customersName = "";
      insertParam.status = 1;
      proTable.value?.getTableList();
    } else {
      ElMessage.error("请填入必填数据");
    }
  });
};

/** 修改客户信息 */
const updateCustomers = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      await editCustomers(updateParam);
      ElMessage.success("修改客户信息成功");
      updateDialogVisible.value = false;
      proTable.value?.getTableList();
    } else {
      ElMessage.error("请填入必填数据");
    }
  });
};
/** 删除客户信息 */
const removeCustomers = async (param: HxCustomers) => {
  await deleteCustomers({ customersId: param.customersId });
};
</script>
